#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //
    createMenu();
    //判断系统是否支持托盘
    if(!QSystemTrayIcon::isSystemTrayAvailable())
    {
        return;
    }
    //实例QSystemTrayIcon
    myTayIcon=new QSystemTrayIcon(this);
    //设置图标
    myTayIcon->setIcon(QIcon(":/a/a"));
    myTayIcon->setToolTip("Qt托盘图标功能");
    myTayIcon->showMessage("托盘","托盘管理",QSystemTrayIcon::Information,10000);
    myTayIcon->setContextMenu(myMenu);
    //显示
    myTayIcon->show();;
}
void MainWindow::createMenu()
{
    restoreWinAction=new QAction("恢复(&R)",this);
    quitAction=new QAction("退出(&Q)",this);
    //恢复
    connect(
                restoreWinAction,
                SIGNAL(triggered(bool)),
                this,
                SLOT(showNormal())
            );
    //退出
    connect(
                quitAction,
                SIGNAL(triggered(bool)),
                this,
                SLOT(showNormal())
            );
    myMenu=new QMenu((QWidget*)QApplication::desktop());
    //添加菜单
    myMenu->addAction(restoreWinAction);
    //分割符合
    myMenu->addSeparator();
    myMenu->addAction(quitAction);
}
//恢复
void MainWindow::showNormal()
{
    this->show();
}
//点击最小化按钮隐藏界面
void QWidget::changeEvent(QEvent * e)
{
    if((e->type()==QEvent::WindowStateChange)&&this->isMinimized())
    {
        //QTimer::singleShot(100,this,SLOT(hide()));
        this->hide();
    }
}
MainWindow::~MainWindow()
{
    delete ui;
}
